<?php


/**
* CakePHP Routines Plugin
* http://cake-routines-plugin.googlecode.com
*
* @package routines
* @subpackage routines.views.helpers
* @author Ivan Chistyakov <zetruger@gmail.com>
* @version $Id: snippet.php 186 2010-11-10 11:42:16Z zetruger@gmail.com $
* @link $HeadURL: http://cake-routines-plugin.googlecode.com/svn/trunk/views/helpers/snippet.php $
* @license MIT License (http://opensource.org/licenses/mit-license.php)
*/


App::import('Helper', 'Html');


class SnippetHelper extends HtmlHelper {

var $helpers = array(
	'Paginator'
);

function link($title, $url = NULL, $options = array(), $confirmMessage = FALSE) {
	$defaults = array(
		'outerSpan' => FALSE,
		'innerSpan' => FALSE
	);
	extract(Set::merge($defaults, $options), EXTR_SKIP);
	unset($options['outerSpan'], $options['innerSpan']);
	if ($outerSpan && $innerSpan) {
		$this->tags['link'] = '<span><a href="%s"%s><span>%s</span></a></span>';
	} elseif ($outerSpan) {
		$this->tags['link'] = '<span><a href="%s"%s>%s</a></span>';
	} elseif ($innerSpan) {
		$this->tags['link'] = '<a href="%s"%s><span>%s</span></a>';
	}
	// class="asc asc"
	if (isset($class)) {
		$options['class'] = implode(' ', array_unique(explode(' ', $class)));
	}
	$link = parent::link($title, $url, $options, $confirmMessage);
	if ($outerSpan || $innerSpan) {
		$this->tags['link'] = '<a href="%s"%s>%s</a>';
	}
	return $link;
}

function sortLink($title, $key = NULL, $options = array()) {
	$defaults = array(
		'model' => NULL,
		'defaultSort' => NULL,
		'defaultDirection' => 'asc'
	);
	extract(Set::merge($defaults, $options), EXTR_SKIP);
	unset($options['defaultSort'], $options['defaultDirection']);
	$currentSort = $this->Paginator->sortKey($model);
	if (empty($currentSort)) {
		$currentSort = $defaultSort;
	}
	$currentDirection = $this->Paginator->sortDir($model);
	if (empty($currentDirection)) {
		$currentDirection = $defaultDirection;
	}
	$options = $this->addClass($options, $currentDirection);
	$defaultModel = $this->Paginator->defaultModel();
	if (($currentSort == $key) || ($currentSort == $defaultModel . '.' . $key) || ($key == $defaultModel . '.' . $currentSort)) {
		$options = $this->addClass($options, 'current');
		$options = $this->addClass($options, 'current-' . $currentDirection);
	} else {
		$options['direction'] = $currentDirection;
	}
	return $this->Paginator->sort($title, $key, $options);
}

function paginatorTable($options = array()) {
	$defaults = array(
		'model' => NULL,
		'titleFirst' => __('<< first', TRUE),
		'titlePrev' => __('<< Previous', TRUE),
		'titleNext' => __('Next >>', TRUE),
		'titleLast' => __('last >>', TRUE),
		'disabledTitle' => TRUE,
		'disabledOptions' => array()
	);
	extract(Set::merge($defaults, $options), EXTR_SKIP);
	unset($options['titleFirst'], $options['titlePrev'], $options['titleNext'], $options['titleLast'], $options['disabledTitle'], $options['disabledOptions']);
	if ($this->Paginator->hasPage($model, 2)) {
		$s = '<table class="paginator">';
		$s .= '<tr>';
		if ($this->Paginator->hasPrev($model)) {
			$s .= '<td class="paginator-first">' . $this->Paginator->first($titleFirst, $options) . '</td>';
			$s .= '<td class="paginator-prev">' . $this->Paginator->prev($titlePrev, $options, $disabledTitle, $disabledOptions) . '</td>';
		} else {
			$s .= '<td class="paginator-first">&nbsp;</td>';
			$s .= '<td class="paginator-prev">&nbsp;</td>';
		}
		$s .= '<td class="paginator-numbers">' . $this->Paginator->numbers($options) . '</td>';
		if ($this->Paginator->hasNext($model)) {
			$s .= '<td class="paginator-next">' . $this->Paginator->next($titleNext, $options, $disabledTitle, $disabledOptions) . '</td>';
			$s .= '<td class="paginator-last">' . $this->Paginator->last($titleLast, $options) . '</td>';
		} else {
			$s .= '<td class="paginator-next">&nbsp;</td>';
			$s .= '<td class="paginator-last">&nbsp;</td>';
		}
		$s .= '</tr>';
		$s .= '</table>';
		return $this->output($s);
	}
	return NULL;
}

function beforeRender() {
	parent::beforeRender();
	// helper injection
	$view = &ClassRegistry::getObject('view');
	if (isset($view->loaded['html']) && !is_a($view->loaded['html'], 'SnippetHelper')) {
		$view->loaded['html'] = &$this;
		$view->Html = &$this;
		foreach (array_keys($view->loaded) as $helper) {
			if (isset($view->loaded[$helper]->Html)) {
				$view->loaded[$helper]->Html = &$this;
			}
		}
	}
}

}


?>